#pragma  once

#include<dao/MachineDao.h>
#include "db/DBRecordset.h"
#include "utils/DBUtils.h"


class  COdbcMachineDao : CMachineDao
{
public:
	COdbcMachineDao()
	{

	}

	~COdbcMachineDao()
	{

	}


	virtual bool GetMachineByID(CMachine& machine)
	{
		CString  strSql;
		CString  sqlFormat = _T( "select * from t_Machine where machineID = '%s';");

		strSql.Format(sqlFormat,machine.m_machineID);
		if(!m_recordSet.OpenTable(strSql) || m_recordSet.IsEOF())
		{
			m_recordSet.CloseRecordSet();
			return false;
		}

		m_recordSet.GetFieldValue("machineID",machine.m_machineID);
		m_recordSet.GetFieldValue("machineName",machine.m_machineName);
		m_recordSet.GetVarIntValue("groupId",&(machine.m_groupId));
		m_recordSet.GetFieldValue("publicKey",machine.m_publicKey);
		m_recordSet.GetFieldValue("privateKey",machine.m_privateKey);
		m_recordSet.GetFieldValue("communicationKey",machine.m_communicationKey);
		m_recordSet.GetFieldValue("remark",machine.m_remark);
		m_recordSet.GetFieldValue("keymod",machine.m_keymod);
		m_recordSet.GetFieldValue("ruleId",machine.m_ruleId);
		m_recordSet.GetFieldValue("newRuleId",machine.m_newRuleId);
		m_recordSet.CloseRecordSet();
		return true;
	}


	virtual  bool  SetCurrentRuleID(CString&  machienId,CString& ruleId) 
	{
		CString  strSql;
		CString  sqlFormat = _T( "update t_machine set ruleId = '%s'  where machineID = '%s'");

		strSql.Format(sqlFormat,ruleId,machienId);

		CDBConnection* pDBConn = CDbUtils::GetConnection();
		if(pDBConn->ExecuteSQL(strSql))
		{
			return true;
		}
		return false;



	}



	virtual bool  UpDateMachineRule(CMachine& machine)
	{

		CString  strSql;
		CString  sqlFormat = _T( "update t_machine set newruleId = '%s'  where machineID = '%s'");

		strSql.Format(sqlFormat,machine.m_newRuleId,machine.m_machineID);

		CDBConnection* pDBConn = CDbUtils::GetConnection();
		if(pDBConn->ExecuteSQL(strSql))
		{
			return true;
		}
		return false;


	}
	virtual bool  UpDateGroupRule(int groupID,CString& ruleId)
	{
		CString  strSql;
		CString  sqlFormat = _T( "update t_machine set newruleId = '%s'  where groupId = %d ");

		strSql.Format(sqlFormat,ruleId,groupID);

		CDBConnection* pDBConn = CDbUtils::GetConnection();
		if(pDBConn->ExecuteSQL(strSql))
		{
			return true;
		}
		return false;
	}

	virtual bool GetNewRuleID(CString&  machienId,CString& newRuleID) 
	{
		CString  strSql;
		CString  sqlFormat = _T("select newRuleID from t_machine   where machineID = '%s'");

		strSql.Format(sqlFormat,machienId);
		if(!m_recordSet.OpenTable(strSql) || m_recordSet.IsEOF())
		{
			m_recordSet.CloseRecordSet();
			return false;
		}
		m_recordSet.GetFieldValue(_T("newRuleID"),newRuleID);
		m_recordSet.CloseRecordSet();
		return true;

	}


	virtual bool GetAllMachine()
	{

		CString  strSql = _T( "select * from t_machine ");

		if(!m_recordSet.OpenTable(strSql) || m_recordSet.IsEOF())
		{
			m_recordSet.CloseRecordSet();
			return false;
		}

		return true;

	}

	virtual bool DelAMachine(CString& machineID)
	{

		CString  strSql;
		CString  sqlFormat = _T( "delete from t_machine  where machineID = '%s'");

		strSql.Format(sqlFormat,machineID);

		CDBConnection* pDBConn = CDbUtils::GetConnection();
		if(pDBConn->ExecuteSQL(strSql))
		{
			return true;
		}
		return false;


	}

	virtual bool AddAMachine(CMachine& machine)
	{
		CString  strSql;
		CString  sqlFormat = _T( "insert into t_Machine(machineID,machineName,groupId,publicKey,privateKey,keymod,communicationKey,remark,ruleId,newRuleId)  values('%s','%s',%d,'%s','%s','%s','%s','%s','%s','%s');");

		strSql.Format(sqlFormat,machine.m_machineID,machine.m_machineName,machine.m_groupId,
			machine.m_publicKey,machine.m_privateKey,machine.m_keymod,machine.m_communicationKey,
			machine.m_remark,machine.m_ruleId,machine.m_newRuleId);

		CDBConnection* pDBConn = CDbUtils::GetConnection();
		if(pDBConn->ExecuteSQL(strSql))
		{
			return true;
		}

		return false;
	}



	virtual bool EnumeMachine(CMachine& machine)
	{

		if(m_recordSet.IsEOF())
		{
			m_recordSet.CloseRecordSet();
			return false;
		}

		m_recordSet.GetFieldValue("machineID",machine.m_machineID);
		m_recordSet.GetFieldValue("machineName",machine.m_machineName);
		m_recordSet.GetVarIntValue("groupId",&(machine.m_groupId));
		m_recordSet.GetFieldValue("publicKey",machine.m_publicKey);
		m_recordSet.GetFieldValue("privateKey",machine.m_privateKey);
		m_recordSet.GetFieldValue("communicationKey",machine.m_communicationKey);
		m_recordSet.GetFieldValue("remark",machine.m_remark);
		m_recordSet.GetFieldValue("keymod",machine.m_keymod);
		m_recordSet.GetFieldValue("ruleId",machine.m_ruleId);
		m_recordSet.GetFieldValue("newRuleId",machine.m_newRuleId);


		m_recordSet.MoveNext();
		return true;


	}

	private:
		CDBRecordset  m_recordSet;

	};